package com.ys.web.background.controller;

import com.ys.utils.base.http.RequestHandler;
import com.ys.utils.base.http.ResponseHandler;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.spring.config.SystemError;
import com.ys.utils.spring.properties.Config;
import com.ys.web.background.service.BackgroundComplainService;
import com.ys.web.background.vo.BackgroundComplainListVo;
import com.ys.web.background.vo.ComplainListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ 名称:
 * @ 创建人: dhf
 * @ 参数： null
 * @ 创建时间: 2022/5/10
 * @ 修改人和其它信息
 * @ 返回值:
 */
@Slf4j
@RestController
@RequestMapping("/manager/complain")
public class BackgroundComplainController {

    @Autowired
    private BackgroundComplainService backgroundComplainService;

    @PostMapping("/list")
    public String getComplainList(HttpServletRequest request, ComplainListVo complainListVo) {
        String ip2 = RequestHandler.getIP(request);
        if(!ip2.equals(Config.pp.get("ys.recharge.refund.ip"))){
            SystemError.wrapBs("400","此ip不在退款白名单中");
        }
        Map<String, Object> result = new HashMap<>();
        int count = backgroundComplainService.getComplainListCount(complainListVo);
        result.put("dataSum", count);
        if (count > 0) {
            List<BackgroundComplainListVo> list = backgroundComplainService.getComplainList(complainListVo);
            result.put("list", list);
        } else {
            result.put("list", new ArrayList<>());
        }
        return ResponseHandler.success(result);
    }

    @PostMapping("/dealComplain")
    public String dealComplain(HttpServletRequest request,String id) {
        String ip2 = RequestHandler.getIP(request);
        if(!ip2.equals(Config.pp.get("ys.recharge.refund.ip"))){
            SystemError.wrapBs("400","此ip不在退款白名单中");
        }
        if (StringHandler.isEmpty(id)) {
            return ResponseHandler.error("500", "参数传递错误");
        }
        //读取投诉
        backgroundComplainService.dealComplain(id, 0);
        return ResponseHandler.success();
    }


    @PostMapping("/freeze")
    public String freeze(HttpServletRequest request,String id) {
        String ip2 = RequestHandler.getIP(request);
        if(!ip2.equals(Config.pp.get("ys.recharge.refund.ip"))){
            SystemError.wrapBs("400","此ip不在退款白名单中");
        }
        if (StringHandler.isEmpty(id)) {
            return ResponseHandler.error("500", "参数传递错误");
        }
        //封禁
        backgroundComplainService.dealComplain(id, 2);
        return ResponseHandler.success();
    }

    @PostMapping("/unfreeze")
    public String unfreeze(HttpServletRequest request,String id) {
        String ip2 = RequestHandler.getIP(request);
        if(!ip2.equals(Config.pp.get("ys.recharge.refund.ip"))){
            SystemError.wrapBs("400","此ip不在退款白名单中");
        }
        if (StringHandler.isEmpty(id)) {
            return ResponseHandler.error("500", "参数传递错误");
        }
        //解封
        backgroundComplainService.dealComplain(id, 1);
        return ResponseHandler.success();
    }

}
